#include "serial_config.h"
#include "ui_serial_config.h"
#include <QDebug>
#include "serial_config_type.h"
struct SerialConfig serial_config_struct;
serial_config::serial_config(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::serial_config)
{
    ui->setupUi(this);
    connect(ui->scan_serial, &QPushButton::clicked, this, &serial_config::scan_btn_click);
    connect(ui->open_serial, &QPushButton::clicked, this, &serial_config::open_serial_btn_click);
}

serial_config::~serial_config()
{
    delete ui;
}


void serial_config::scan_btn_click()
{
    const auto serialPortInfos = QSerialPortInfo::availablePorts();
    if (serialPortInfos.isEmpty()) {
        qDebug() << "未发现可用串口。";
        ui->port->clear();
    } else {
        qDebug() << "发现" << serialPortInfos.size() << "个串口：";
        for (const QSerialPortInfo &info : serialPortInfos) {
            qDebug() << "---------------------------";
            qDebug() << "名称："       << info.portName();
            qDebug() << "描述："       << info.description();
            qDebug() << "制造商："     << info.manufacturer();
            qDebug() << "串口系统路径：" << info.systemLocation();
            qDebug() << "供应商ID："   << (info.hasVendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : "无");
            qDebug() << "产品ID："     << (info.hasProductIdentifier() ? QString::number(info.productIdentifier(), 16) : "无");
            QString itemText = info.portName() + " (" + info.description() + ")";
            if (ui->port->findText(itemText) == -1) {
                ui->port->addItem(itemText);
            }
        }
    }
    qDebug() << "scan_btn_click!";
}

void serial_config::open_serial_btn_click()
{
    if (serial.isOpen())
    {
        serial.close();
    }

    QString portName = ui->port->currentData().toString();
    if (portName.isEmpty())
        portName = ui->port->currentText().split(" ").first();

    // serial.setPortName(portName);
    // serial.setBaudRate(QSerialPort::Baud9600);
    // serial.setDataBits(QSerialPort::Data8);
    // serial.setParity(QSerialPort::NoParity);
    // serial.setStopBits(QSerialPort::OneStop);
    // serial.setFlowControl(QSerialPort::NoFlowControl);



    // if (serial.open(QIODevice::ReadWrite)) {
    //     qDebug() << "串口打开成功：" << portName;
    //     emit serial_port_selected(portName);  // 发射信号
        serial_config_struct.portName = portName;
        serial_config_struct.baudRate = ui->baterate->currentText().toInt();
        int index = ui->parity->currentIndex();

        switch (index) {
        case 0:
            serial_config_struct.parity = QSerialPort::NoParity;
            break;
        case 1:
            serial_config_struct.parity = QSerialPort::OddParity;
            break;
        case 2:
            serial_config_struct.parity = QSerialPort::EvenParity;
            break;
        default:
            serial_config_struct.parity = QSerialPort::UnknownParity; // 可选：处理非法情况
            break;
        }

        index = ui->databits->currentIndex();

        switch (index) {
        case 0:
            serial_config_struct.dataBits = QSerialPort::Data8;
            break;
        case 1:
            serial_config_struct.dataBits = QSerialPort::Data7;
            break;
        case 2:
            serial_config_struct.dataBits = QSerialPort::Data6;
            break;
        case 3:
            serial_config_struct.dataBits = QSerialPort::Data5;
            break;
        default:
            serial_config_struct.dataBits = QSerialPort::UnknownDataBits; // 可选：处理非法情况
            break;
        }
        index = ui->stopbits->currentIndex();

        switch (index) {
        case 0:
            serial_config_struct.stopBits = QSerialPort::OneStop;
            break;
        case 1:
            serial_config_struct.stopBits = QSerialPort::TwoStop;
            break;
        case 2:
            serial_config_struct.stopBits = QSerialPort::OneAndHalfStop;
            break;
        default:
            serial_config_struct.stopBits = QSerialPort::UnknownStopBits;
            break;
        }
        emit serial_port_selected(portName);  // 发出信号
        QMessageBox::information(this, "成功", "串口配置保存成功：");
    // } else {
    //     qDebug() << "串口打开失败：" << serial.errorString();
    //     QMessageBox::critical(this, "错误", "串口打开失败：\n" + serial.errorString());
    //     emit serial_port_selected("");  // 发射信号
    // }
}


